package com.person.jc.service.base.device;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.Service;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.SessionKit;
import com.person.jc.utils.Utils;

public class BdDevicePartsService extends Service {
	
	public final static BdDevicePartsService me = new BdDevicePartsService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		
		if(Utils.isNotEmpty(String.valueOf(map.get("partsIds")))){
			temp = "(select p.parts_id from bd_device_parts p where t.parts_id = p.parts_id and p.parts_id in("+map.get("partsIds")+")) flag,";
		}
		
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("select t.*, "+temp+" date_format(storage_time, '%Y-%m-%d %H:%i:%s') storage_time_ from bd_device_parts t where if_valid = 1 ");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("partsCode")))){
			sql.append(" and t.parts_code like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("partsName")))){
			sql.append(" and t.parts_name like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("partsOper")))){
			sql.append(" and t.parts_oper like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("startDate")))){
			sql.append(" and date_format(storage_time, '%Y-%m-%d') >= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("endtDate")))){
			sql.append(" and date_format(storage_time, '%Y-%m-%d') <= ?");
			params.add(temp);
		}
		if("change".equals(map.get("op"))){
			sql.append(" and (parts_status = " + map.get("partsStatus")).append(" or t.parts_id in ("+map.get("partsIds")+")) ");
		}else{
			if(Utils.isNotEmpty(temp = String.valueOf(map.get("partsStatus")))){
				sql.append(" and parts_status = " + temp);
			}
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("partsId")))){
			sql.append(" and t.parts_id = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("otherIds")))){
			if(temp.endsWith(",")){
				temp = temp.substring(0, temp.lastIndexOf(","));
			}
			sql.append(" and t.parts_id not in ("+ temp +")");
		}
		sql.append(" order by t.create_time desc, t.parts_name ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		String sql = "insert into bd_device_parts (parts_code, parts_name, parts_spec, parts_num, storage_time, parts_oper, remark, create_time) values (?, ?, ?, ?, ?, ?, ?, now())";
		Db.update(sql, map.get("partsCode"), map.get("partsName"), map.get("partsSpec"), map.get("partsNum"), map.get("storageTime"), map.get("partsOper"), map.get("remark"));
		super.setCRUD("设备配件管理", "C", "新增设备配件，名称："+map.get("deviceName"));
	}
	
	public void update(Map<String,Object> map){
		String sql = " update bd_device_parts set parts_code = ?, parts_name = ?, parts_spec = ?, parts_num = ?, storage_time = ?, parts_oper = ?, remark = ? where parts_id = ?";
		Db.update(sql, map.get("partsCode"), map.get("partsName"), map.get("partsSpec"), map.get("partsNum"), map.get("storageTime"), map.get("partsOper"), map.get("remark"), map.get("partsId"));
		super.setCRUD("设备配件管理", "U", "修改设备配件，编号："+map.get("partsId"));
	}
	
	/**
	 * 更改配件状态
	 * @作者 : 王春林（chunlin.wang@51durian.com）
	 * @日期 : 2017年2月7日
	 * @param partsId 数组中多个ID以逗号隔开
	 * @param status
	 */
	public void update(String[] partsId, int status){
		for (String pid : partsId) {
			String sql = "update bd_device_parts set parts_status = ? where parts_id in ("+pid+")";
			Db.update(sql, status);
			super.setCRUD("设备配件管理", "U", "修改设备配件状态，编号："+pid);
		}
	}
	
	public void delete(String partsId){
		String sql = " update bd_device_parts set if_valid = 0 where parts_id = ? ";
		Db.update(sql, partsId);
		super.setCRUD("设备配件管理", "D", "删除设备配件，编号："+partsId);
	}
	
	public long checkName(String partsCode, String partsId){
		String sql = "select count(1) from bd_device_parts where parts_code = ? and if_valid <> 0 ";
		if(Utils.isNotEmpty(partsId)){
			sql += " and parts_id <> " + partsId;
		}
		return Db.queryLong(sql, partsCode);
	}
	
	public void saveOutLog(Map<String,Object> map){
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		Record outRecord = new Record();
		outRecord.set("parts_id_", map.get("partsId"));
		outRecord.set("use_time", DateUtils.getDateTime());
		outRecord.set("use_by", tsUser.getUserId());
		outRecord.set("use_to", map.get("useTo"));
		Db.save("bd_device_parts_outlog", outRecord);
		super.setCRUD("设备出库", "C", "新增出库记录，设备编号：" + map.get("parts_id"));
	}
}
